package com.wpp;

import org.junit.Test;

import javax.xml.transform.Source;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class test {

    public static void main(String args[]) {
        //第一种
        CountTest countTest = new CountTest();
        Class<? extends CountTest> aClass = countTest.getClass();
        System.out.println(aClass);

        //获取的所有构造器
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();

        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }

        try {
            Method test1 = aClass.getMethod("test");
            CountTest countTest1 = aClass.newInstance();
            test1.invoke(countTest1);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        System.out.println("----------------------------------------");
        //第二种
        Class<CountTest> countTestClass = CountTest.class;
        System.out.println(countTestClass);

        //第三种，常用的
        try {
            Class<?> aClass1 = Class.forName("com.wpp.CountTest");

            System.out.println(aClass1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //第四种
        ClassLoader classLoader = test.class.getClassLoader();
        System.out.println(classLoader);
        try {
            Class<?> aClass2 = classLoader.loadClass("com.wpp.CountTest");
            System.out.println(aClass2);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
